﻿using RXY_Mall.Api.Common;
using RXY_Mall.Api.Models;
using RXY_Mall.Core;
using RXY_Mall.Core.Tool;
using RXY_Mall.Data;
using RXY_Mall.Domain;
using RXY_Mall.Services;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace RXY_Mall.Api.Controllers
{
    /// <summary>
    /// 退换货接口
    /// </summary>
    [RoutePrefix("Return")]
    [ApiSecurityFilter]
    public class ReturnController : BaseApiController
    {
        private RXY_MallContext db;
        private OrderService orderService;

        public ReturnController()
        {
            db = new RXY_MallContext();
            orderService = new OrderService();
        }

        /// <summary>
        /// 添加退换货（第4批）
        /// </summary>
        [HttpPost]
        [Route("AddReturn")]
        public async Task<Response<bool>> AddReturn(ReturnInput input)
        {
            Response<bool> result = new Response<bool>();
            result.code = 0;
            result.msg = "";
            result.data = false;

            try
            {
                if (input.pictures != null && input.pictures.Count > 3)
                {
                    result.msg = "最多上传3张图片";
                    return result;
                }

                //判断订单是否存在
                var isOrder = await orderService.IsExistOrder(input.order_no);
                if (!isOrder)
                {
                    result.msg = "订单不存在";
                    return result;
                }

                var order = await db.gw_orders
                    .Include(o => o.gw_order_goods)
                    .Include(o => o.gw_order_address)
                    .FirstOrDefaultAsync(o => o.order_no == input.order_no);

                if (order == null || order.user_id != input.user_id && order.deleted)
                {
                    result.msg = "订单不存在";
                    return result;
                }

                List<int> lstOrderStatus = new List<int>();
                lstOrderStatus.Add((int)OrderStatus.Complete);
                lstOrderStatus.Add((int)OrderStatus.Comment);

                if (!lstOrderStatus.Contains(order.status))
                {
                    result.msg = "订单尚未完成";
                    return result;
                }

                if (order.service_status == (int)ServiceStatus.售后过期 
                    || (order.service_status == (int)ServiceStatus.可申请售后 && DateTime.Now > order.service_expire_time))
                {
                    result.msg = "该商品已超过售后期";
                    return result;
                }

                if (order.service_status == (int)ServiceStatus.售后中)
                {
                    result.msg = "售后中";
                    return result;
                }

                if (order.service_status > (byte)ServiceStatus.售后关闭)
                {
                    result.msg = "售后关闭";
                    return result;
                }

                //判断是否已创建退换货订单
                var flag = await db.gw_returns
                    .AnyAsync(r => r.order_id == order.id && r.deleted == false
                    && r.complete_time == null && r.cancel_time == null);
                if (flag)
                {
                    result.msg = "该订单已申请退换货";
                    return result;
                }

                order.service_status = (int)ServiceStatus.售后中;
                db.Entry(order).Property(o => o.service_status).IsModified = true;

                var returnType = "";
                switch (input.return_type)
                {
                    case ReturnType.GoodsAndMoney:
                        returnType = "GM";
                        break;
                    case ReturnType.Money:
                        returnType = "M";
                        break;
                    case ReturnType.Goods:
                    default:
                        returnType = "G";
                        break;
                }

                if (order.gw_order_goods.goods_type == (int)GoodsType.Free)
                {
                    if (input.return_type != ReturnType.Goods)
                    {
                        result.msg = "合体免单商品只能换货";
                        return result;
                    }
                }

                if (order.gw_order_goods.goods_type == (int)GoodsType.Ordinary
                    && order.gw_order_goods.buy_type == (int)BuyType.GroupBuy)
                {
                    if (input.return_type != ReturnType.Goods)
                    {
                        result.msg = "拼手气商品只能换货";
                        return result;
                    }
                }

                switch (input.return_reason)
                {
                    case ReturnReason.SevenDaysWithoutReason:
                        break;
                    case ReturnReason.NoWant:
                        break;
                    case ReturnReason.NoLike:
                        break;
                    case ReturnReason.DescribeDiscrepancy:
                        break;
                    case ReturnReason.Expire:
                        break;
                    case ReturnReason.Other:
                    default:
                        input.return_reason = ReturnReason.Other;
                        break;
                }

                //退换货
                var return_no = string.Format("R{0}{1}", returnType, Utils.GetOrderNumber());
                var returnOrder = new gw_return()
                {
                    add_time = DateTime.Now,
                    shop_id = order.gw_order_goods.shop_id,
                    user_id = order.user_id,
                    goods_count = order.goods_count,
                    order_amount = order.amount,
                    order_id = order.id,
                    return_no = return_no,
                    status = (int)ReturnStatus.提交申请,
                    expire_time = DateTime.Now.AddDays(14),
                };

                //退换货商品
                var return_goods = new gw_return_goods()
                {
                    buy_type = order.gw_order_goods.buy_type,
                    category_id = order.gw_order_goods.category_id,
                    goods_id = order.gw_order_goods.goods_id,
                    goods_name = order.gw_order_goods.goods_name,
                    goods_type = order.gw_order_goods.goods_type,
                    picture_url = order.gw_order_goods.picture_url,
                    pay_price = order.gw_order_goods.pay_price,
                    quantity = order.gw_order_goods.quantity,
                    shop_id = order.gw_order_goods.shop_id,
                    sku_id = order.gw_order_goods.sku_id,
                    sku_specs = order.gw_order_goods.sku_specs,
                };

                returnOrder.gw_return_goods = return_goods;

                //退换货请求
                var return_request = new gw_return_request()
                {
                    remark = input.remark,
                    add_time = DateTime.Now,
                    contact_mobile = order.gw_order_address.mobile,
                    contact_name = order.gw_order_address.accepter_name,
                    refund_type = (int)RefundType.退款到余额,
                    return_reason = (byte)input.return_reason,
                    return_type = (byte)input.return_type,
                };
                returnOrder.gw_return_request = return_request;

                //退换货请求照片
                if (input.pictures != null && input.pictures.Count > 0)
                {
                    for (int i = 0; i < input.pictures.Count; i++)
                    {
                        var gw_return_request_picture = new gw_return_request_picture()
                        {
                            sort = i,
                            url = input.pictures[i],
                        };
                        return_request.gw_return_request_picture.Add(gw_return_request_picture);
                    }
                }

                var returns = await db.gw_returns.Where(r => r.order_id == order.id && r.user_id == input.user_id && r.deleted == false).ToListAsync();

                if (returns != null && returns.Count > 0)
                {
                    foreach (var item in returns)
                    {
                        item.deleted = true;
                        db.Entry(item).Property(r => r.deleted).IsModified = true;
                    }
                }

                db.gw_returns.Add(entity: returnOrder);

                await db.SaveChangesAsync();

                result.code = 1;
                result.data = true;
                result.msg = "申请成功";
            }
            catch (Exception ex)
            {
                result.code = 2;
                result.msg = ex.ToString();
                LogHelper.WriteLog(ex);
            }
            return result;
        }

        /// <summary>
        /// 获得退换货列表（第4批）
        /// </summary>
        /// <param name="user_id">用户ID</param>
        /// <param name="process_state">处理状态</param>
        /// <param name="page_index">页码</param>
        /// <param name="page_size">页数</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetReturnList")]
        public Response<IPagedList<GetReturnListOutput>> GetReturnList(int user_id, ProcessState process_state = ProcessState.All, int page_index = 1, int page_size = 20)
        {
            Response<IPagedList<GetReturnListOutput>> result = new Response<IPagedList<GetReturnListOutput>>();
            result.code = 0;
            result.msg = "";

            try
            {
                var gw_returns = db.gw_returns
                    .Include(r => r.gw_return_request)
                    .Include(r => r.gw_return_goods)
                    .Include(r => r.gw_return_goods.gw_goods.gw_shop)
                    .Where(r => r.user_id == user_id && r.deleted == false);

                if (process_state == ProcessState.Process)
                {
                    List<int> lstStatus = new List<int>();

                    lstStatus.Add((int)ReturnStatus.已完成);
                    lstStatus.Add((int)ReturnStatus.已取消);
                    lstStatus.Add((int)ReturnStatus.审核失败);

                    gw_returns = gw_returns.Where(r => !lstStatus.Contains(r.status));
                }

                var sources = gw_returns.Select(r => new GetReturnListOutput()
                {
                    add_time = r.add_time,
                    json_specs = r.gw_return_goods.sku_specs,

                    return_no = r.return_no,
                    return_status = (ReturnStatus)r.status,
                    return_type = (ReturnType)r.gw_return_request.return_type,

                    order_amount = r.order_amount.ToString(),
                    return_amount = r.refund_amount.ToString(),

                    shop = new ShopOutput()
                    {
                        shop_id = r.gw_return_goods.gw_goods.gw_shop.id,
                        shop_logo = r.gw_return_goods.gw_goods.gw_shop.logo,
                        shop_name = r.gw_return_goods.gw_goods.gw_shop.name,
                    },

                    goods = new GoodsOutput()
                    {
                        goods_id = r.gw_return_goods.goods_id,
                        goods_name = r.gw_return_goods.goods_name,
                        goods_type = (GoodsType)r.gw_return_goods.goods_type,
                        goods_quantity = r.gw_return_goods.quantity,
                        goods_price = r.gw_return_goods.pay_price.ToString(),
                        picture_url = r.gw_return_goods.picture_url,
                    },
                });

                sources = sources.OrderByDescending(s => s.add_time);
                IPagedList<GetReturnListOutput> pagelist = new PagedList<GetReturnListOutput>(sources, page_index, page_size);

                foreach (var item in pagelist)
                {
                    item.goods.picture_url = HostHelper.GetImgUrl(item.goods.picture_url);
                    item.shop.shop_logo = HostHelper.GetImgUrl(item.shop.shop_logo);
                    item.goods.specs = JsonConvert.DeserializeObject<List<SpecOutput>>(item.json_specs);
                    item.create_time = TimeHelper.GetTimeStamp(item.add_time);
                }

                result.data = pagelist;
                result.code = 1;
            }
            catch (Exception ex)
            {
                result.code = 2;
                result.msg = ex.ToString();
                LogHelper.WriteLog(ex);
            }
            return result;
        }

        /// <summary>
        /// 获得订单详情（第4批）
        /// </summary>
        /// <param name="user_id">用户ID</param>
        /// <param name="return_no">订单号</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetReturn")]
        public async Task<Response<GetReturnOutput>> GetReturn(int user_id, string return_no)
        {
            Response<GetReturnOutput> result = new Response<GetReturnOutput>();
            result.code = 0;
            result.msg = "";

            try
            {
                var order = await db.gw_returns
                    .Include(r => r.gw_order)
                    .Include(r => r.gw_return_request)
                    .Include(o => o.gw_return_goods)
                    .Include(o => o.gw_return_goods.gw_goods.gw_shop)
                    .Include(o => o.gw_return_audit)
                    .Include(o => o.gw_return_waybill)
                    .Where(o => o.user_id == user_id && o.deleted == false && o.return_no == return_no)
                    .Select(r => new GetReturnOutput()
                    {
                        add_time = r.add_time,
                        expire_time = r.expire_time,
                        audit_pass_time = r.audit_pass_time,
                        audit_fail_time = r.audit_fail_time,
                        user_shipment_time = r.user_shipment_time,
                        shop_receive_time = r.shop_receive_time,
                        shop_refund_time = r.shop_refund_time,
                        shop_shipment_time = r.shop_shipment_time,
                        user_receive_time = r.user_receive_time,
                        cancel_time = r.cancel_time,
                        complete_time = r.complete_time,

                        return_no = r.return_no,
                        order_no = r.gw_order.order_no,
                        return_status = (ReturnStatus)r.status,
                        order_amount = r.order_amount.ToString(),
                        refund_amount = r.refund_amount.ToString(),

                        buyer_waybill = r.gw_return_waybill.Where(w => w.sender_type == 1).Select(w => new Waybill()
                        {
                            company_name = w.company_name,
                            telphone = w.telphone,
                            waybill_no = w.waybill_no,
                        }).FirstOrDefault(),

                        merchant_waybill = r.gw_return_waybill.Where(w => w.sender_type == 2).Select(w => new Waybill()
                        {
                            company_name = w.company_name,
                            telphone = w.telphone,
                            waybill_no = w.waybill_no,
                        }).FirstOrDefault(),

                        goods = new OrderGoods()
                        {
                            category_id = r.gw_return_goods.gw_goods.category_id,

                            goods_id = r.gw_return_goods.goods_id,
                            goods_name = r.gw_return_goods.goods_name,
                            goods_type = (GoodsType)r.gw_return_goods.goods_type,
                            json_specs = r.gw_return_goods.sku_specs,

                            buy_type = (BuyType)r.gw_return_goods.buy_type,
                            goods_quantity = r.gw_return_goods.quantity,
                            goods_price = r.gw_return_goods.pay_price.ToString(),
                            picture_url = r.gw_return_goods.picture_url,
                        },

                        shop = new ShopOutput()
                        {
                            shop_id = r.gw_return_goods.gw_goods.gw_shop.id,
                            shop_logo = r.gw_return_goods.gw_goods.gw_shop.logo,
                            shop_name = r.gw_return_goods.gw_goods.gw_shop.name,
                        },

                        request = new ReturnRequest()
                        {
                            contact_mobile = r.gw_return_request.contact_mobile,
                            contact_name = r.gw_return_request.contact_name,

                            remark = r.gw_return_request.remark,
                            return_reason = (ReturnReason)r.gw_return_request.return_reason,
                            return_type = (ReturnType)r.gw_return_request.return_type,
                        },

                        gw_return_audit = r.gw_return_audit,

                    }).FirstOrDefaultAsync();

                //订单
                if (order != null)
                {
                    order.return_cancel_time = TimeHelper.GetTimeStamp(order.cancel_time);
                    order.return_complete_time = TimeHelper.GetTimeStamp(order.complete_time);
                    order.return_create_time = TimeHelper.GetTimeStamp(order.add_time);
                    order.return_expire_time = TimeHelper.GetTimeStamp(order.expire_time);
                    order.merchant_audit_pass_time = TimeHelper.GetTimeStamp(order.audit_pass_time);
                    order.merchant_audit_fail_time = TimeHelper.GetTimeStamp(order.audit_fail_time);
                    order.merchant_receive_time = TimeHelper.GetTimeStamp(order.shop_receive_time);
                    order.merchant_refund_time = TimeHelper.GetTimeStamp(order.shop_refund_time);
                    order.merchant_shipment_time = TimeHelper.GetTimeStamp(order.shop_shipment_time);
                    order.buyer_receiver_time = TimeHelper.GetTimeStamp(order.user_receive_time);
                    order.buyer_shipment_time = TimeHelper.GetTimeStamp(order.user_shipment_time);

                    order.goods.picture_url = HostHelper.GetImgUrl(order.goods.picture_url);
                    order.shop.shop_logo = HostHelper.GetImgUrl(order.shop.shop_logo);

                    order.process.Add(new ProcessItem()
                    {
                        title = "您的申请已提交",
                        content = order.request.remark,
                        date = order.return_create_time,
                    });

                    if (order.merchant_audit_pass_time != 0 || order.merchant_audit_fail_time != 0)
                    {
                        string title = "通过";
                        if (order.gw_return_audit != null)
                        {
                            order.audit = new ReturnAudit()
                            {
                                add_time = TimeHelper.GetTimeStamp(order.gw_return_audit.add_time),
                                is_pass = order.gw_return_audit.is_pass,
                                audit_note = order.gw_return_audit.audit_note,
                                remark = order.gw_return_audit.remark,
                            };
                            title = order.audit.is_pass ? "通过" : "未通过";

                            order.process.Add(new ProcessItem()
                            {
                                title = string.Format("商家审核{0}了您的申请", title),
                                content = order.audit.audit_note + "\n" + order.audit.remark,
                                date = order.merchant_audit_pass_time,
                            });
                        }
                    }
                    if (order.buyer_shipment_time != 0)
                    {
                        order.process.Add(new ProcessItem()
                        {
                            title = "退货信息",
                            content = string.Format("物流公司:{0}\n物流单号:{1}", order.buyer_waybill.company_name, order.buyer_waybill.waybill_no),
                            date = order.buyer_shipment_time,
                        });
                    }
                    if (order.merchant_receive_time != 0)
                    {
                        order.process.Add(new ProcessItem()
                        {
                            title = "商家已确认收货",
                            date = order.merchant_receive_time,
                        });
                    }
                    switch (order.request.return_type)
                    {
                        case ReturnType.GoodsAndMoney:
                        case ReturnType.Money:
                            if (order.merchant_refund_time != 0)
                            {
                                order.process.Add(new ProcessItem()
                                {
                                    title = "退款受理中",
                                    content = string.Format("退款商品:{0}\n退款金额:{1}\n到账时间:7个工作日之内", order.goods.goods_name, order.refund_amount),
                                    date = order.merchant_refund_time,
                                });
                            }
                            break;
                        case ReturnType.Goods:
                            if (order.merchant_shipment_time != 0)
                            {
                                order.process.Add(new ProcessItem()
                                {
                                    title = "商家重新发货",
                                    content = string.Format("物流公司:{0}\n物流单号:{1}", order.merchant_waybill.company_name, order.merchant_waybill.waybill_no),
                                    date = order.merchant_shipment_time,
                                });
                            }
                            if (order.buyer_receiver_time != 0)
                            {
                                order.process.Add(new ProcessItem()
                                {
                                    title = "买家确认收货",
                                    date = order.buyer_receiver_time,
                                });
                            }
                            break;
                    }

                    if (order.return_complete_time != 0)
                    {
                        order.process.Add(new ProcessItem()
                        {
                            title = "已完成",
                            date = order.return_complete_time,
                        });
                    }

                    if (order.return_cancel_time != 0)
                    {
                        order.process.Add(new ProcessItem()
                        {
                            title = "已取消",
                            date = order.return_cancel_time,
                        });
                    }

                }

                result.code = 1;
                result.data = order;
            }
            catch (Exception ex)
            {
                result.code = 2;
                result.msg = ex.ToString();
                LogHelper.WriteLog(ex);
            }
            return result;
        }

        /// <summary>
        /// 取消退换货（第4批）
        /// </summary>
        [HttpPost]
        [Route("CancelReturn")]
        public async Task<Response<bool>> CancelReturn(CancelReturnInput input)
        {
            Response<bool> result = new Response<bool>();
            result.code = 0;
            result.msg = "";
            result.data = false;

            try
            {
                //判断退换货是否存在
                var order = await db.gw_returns
                    .FirstOrDefaultAsync(o => o.return_no == input.return_no && o.user_id == input.user_id && o.deleted == false);
                if (order == null)
                {
                    result.msg = "退换单不存在";
                }

                if (order.status == (int)ReturnStatus.提交申请)
                {
                    order.status = (int)OrderStatus.Cancel;
                    order.cancel_time = DateTime.Now;
                    db.Entry(order).Property(o => o.status).IsModified = true;
                    db.Entry(order).Property(o => o.cancel_time).IsModified = true;
                    await db.SaveChangesAsync();

                    result.code = 1;
                    result.data = true;
                    result.msg = "取消成功";
                    return result;
                }
                else
                {
                    result.msg = "退换单已审核或已取消";
                }
            }
            catch (Exception ex)
            {
                result.code = 2;
                result.msg = ex.ToString();
                LogHelper.WriteLog(ex);
            }
            return result;
        }

        /// <summary>
        /// 确认收货（第4批）
        /// </summary>
        [HttpPost]
        [Route("ReceiveReturn")]
        public async Task<Response<bool>> ReceiveReturn(CancelReturnInput input)
        {
            Response<bool> result = new Response<bool>();
            result.code = 0;
            result.msg = "";
            result.data = false;

            try
            {
                var service = await db.gw_returns
                    .Include(r => r.gw_order)
                    .FirstOrDefaultAsync(o => o.return_no == input.return_no && o.user_id == input.user_id && o.deleted == false);
                if (service == null)
                {
                    result.msg = "退换单不存在";
                }

                if (service.status == (int)ReturnStatus.商家重新发货)
                {
                    service.status = (int)ReturnStatus.买家确认收货;
                    service.user_receive_time = DateTime.Now;
                    db.Entry(service).Property(o => o.status).IsModified = true;
                    db.Entry(service).Property(o => o.user_receive_time).IsModified = true;

                    service.status = (int)ReturnStatus.已完成;
                    service.complete_time = DateTime.Now;
                    db.Entry(service).Property(o => o.status).IsModified = true;
                    db.Entry(service).Property(o => o.complete_time).IsModified = true;

                    service.gw_order.service_status = (byte)ServiceStatus.售后中;
                    service.gw_order.service_expire_time = DateTime.Now.AddDays(7);
                    db.Entry(service.gw_order).Property(o => o.service_status).IsModified = true;
                    db.Entry(service.gw_order).Property(o => o.service_expire_time).IsModified = true;

                    await db.SaveChangesAsync();

                    result.code = 1;
                    result.data = true;
                    return result;
                }
                else
                {
                    result.msg = "售后未完成";
                }
            }
            catch (Exception ex)
            {
                result.code = 2;
                result.msg = ex.ToString();
                LogHelper.WriteLog(ex);
            }
            return result;
        }

        /// <summary>
        /// 删除退换货（第4批）
        /// </summary>
        [HttpPost]
        [Route("DeleteReturn")]
        public async Task<Response<bool>> DeleteReturn(CancelReturnInput input)
        {
            Response<bool> result = new Response<bool>();
            result.code = 0;
            result.msg = "";
            result.data = false;

            try
            {
                var returnOrder = await db.gw_returns
                    .FirstOrDefaultAsync(o => o.return_no == input.return_no && o.user_id == input.user_id && o.deleted == false);
                if (returnOrder == null)
                {
                    result.msg = "退换单不存在";
                    return result;
                }

                List<int> lst = new List<int>();
                lst.Add((int)ReturnStatus.已取消);
                lst.Add((int)ReturnStatus.已完成);
                lst.Add((int)ReturnStatus.审核失败);

                if (lst.Contains(returnOrder.status))
                {
                    result.msg = "退换单正在进行中,不能删除";

                    returnOrder.deleted = true;

                    await db.SaveChangesAsync();

                    result.code = 1;
                    result.data = true;
                    result.msg = "删除成功";
                    return result;
                }
                else
                {
                    result.msg = "正在处理中";
                }
            }
            catch (Exception ex)
            {
                result.code = 2;
                result.msg = ex.ToString();
                LogHelper.WriteLog(ex);
            }
            return result;
        }

        /// <summary>
        /// 填写运单信息（第4批）
        /// </summary>
        [HttpPost]
        [Route("AddWaybill")]
        public async Task<Response<bool>> AddWaybill(AddWaybillInput input)
        {
            Response<bool> result = new Response<bool>();
            result.code = 0;
            result.msg = "";
            result.data = false;

            try
            {
                var returnOrder = await db.gw_returns
                    .Include(r => r.gw_return_request)
                    .Include(r => r.gw_return_waybill)
                    .FirstOrDefaultAsync(o => o.return_no == input.return_no && o.user_id == input.user_id && o.deleted == false);
                if (returnOrder == null)
                {
                    result.msg = "退换单不存在";
                    return result;
                }

                if (returnOrder.gw_return_request.return_type == (int)ReturnType.Money || returnOrder.status != (int)ReturnStatus.审核通过)
                {
                    result.msg = "请耐心等待";
                    return result;
                }

                bool flag = returnOrder.gw_return_waybill.Any(w => w.return_id == returnOrder.id && w.sender_type == (int)SenderType.Buyer);
                if (flag)
                {
                    result.msg = "已添加运单";
                    return result;
                }

                if (returnOrder.status != (int)ReturnStatus.审核通过)
                {
                    result.msg = "审核中";
                    return result;
                }

                returnOrder.user_shipment_time = DateTime.Now;
                returnOrder.status = (int)ReturnStatus.退货信息;
                db.Entry(returnOrder).Property(r => r.user_shipment_time).IsModified = true;
                db.Entry(returnOrder).Property(r => r.status).IsModified = true;

                gw_return_waybill waybill = new gw_return_waybill()
                {
                    add_time = DateTime.Now,
                    company_name = input.company_name,
                    return_id = returnOrder.id,
                    sender_type = (int)SenderType.Buyer,
                    waybill_no = input.waybill_no,
                };

                db.gw_return_waybills.Add(waybill);

                await db.SaveChangesAsync();

                result.code = 1;
                result.msg = "添加成功";
            }
            catch (Exception ex)
            {
                result.code = 2;
                result.msg = ex.ToString();
                LogHelper.WriteLog(ex);
            }
            return result;
        }

        ///// <summary>
        ///// 已完成（第4批）
        ///// </summary>
        //[HttpPost]
        //[Route("CompleteReturn")]
        //public async Task<Response<bool>> CompleteReturn(CancelReturnInput input)
        //{
        //    Response<bool> result = new Response<bool>();
        //    result.code = 0;
        //    result.msg = "";
        //    result.data = false;

        //    try
        //    {
        //        var returnOrder = await db.gw_returns
        //            .FirstOrDefaultAsync(o => o.return_no == input.return_no && o.user_id == input.user_id && o.deleted == false);
        //        if (returnOrder == null)
        //        {
        //            result.msg = "退换单不存在";
        //        }

        //        if (returnOrder.status == (int)ReturnStatus.商家重新发货)
        //        {
        //            returnOrder.status = (int)ReturnStatus.买家确认收货;
        //            returnOrder.user_receive_time = DateTime.Now;
        //            db.Entry(returnOrder).Property(o => o.status).IsModified = true;
        //            db.Entry(returnOrder).Property(o => o.user_receive_time).IsModified = true;

        //            await db.SaveChangesAsync();

        //            result.code = 1;
        //            result.data = true;
        //            return result;
        //        }
        //        else
        //        {
        //            result.msg = "退换货状态不正确";
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        result.code = 2;
        //        result.msg = ex.ToString();
        //        LogHelper.WriteLog(ex);
        //    }
        //    return result;
        //}


    }
}
